package com.shycloud.mido.organ.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.organ.api.vo.OfficeStudentDetailVO;
import com.shycloud.mido.organ.service.OfficeService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工作室相关业务Controller
 *
 * @author nianhua.jiang
 * @date 2020/6/3
 */
@RestController
@AllArgsConstructor
@RequestMapping("/office")
public class OfficeController {

	/** 机构模块 service */
	private final OfficeService officeService;

	/**
	 * 分页获取工作室信息
	 *
	 * @param current
	 * @param size
	 * @param officeName
	 * @param teacherName
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/3 10:06
	 */
	@GetMapping("getOffice")
	@ApiOperation("分页获取工作室信息")
	public R getOffice(Integer current, Integer size, String officeName, String teacherName) {

		//分页参数
		Page page = new Page(current, size);

		return new R(officeService.getOffice(page, officeName, teacherName));

	}

	/**
	 * 查询工作室学生数量
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/4 9:41
	 */
	@GetMapping("getOfficeStudentCount")
	@ApiOperation("查询工作室学生数量")
	public R getOfficeStudentCount() {
		return new R(officeService.getOfficeStudentCount());
	}

	/**
	 * 查询工作室下的学生信息
	 *
	 * @param current
	 * @param size
	 * @param phone
	 * @param studentName
	 * @param officeId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/4 13:26
	 */
	@GetMapping("getOfficeStudentDetail")
	@ApiOperation("查询工作室下的学生信息")
	public R getOfficeStudentDetail(Integer current, Integer size, String phone, String studentName, String officeId) {

		Page page = new Page(current, size);

		return new R(officeService.getOfficeStudentDetail(page, phone, studentName, officeId));
	}

	/**
	 * 批量从工作室移除学生
	 *
	 * @param request
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/4 14:46
	 */
	@PostMapping("removeofficeStudent")
	@ApiOperation("批量从工作室移除学生")
	public R removeofficeStudent(@RequestBody Map<String, Object> request) {

		//工作室ID
		Integer officeId = (Integer) request.get("officeId");
		//学生ID 集合
		List<String> studentId = (List<String>) request.get("studentIdList");

		return new R(officeService.removeofficeStudent(officeId, studentId));
	}

	/**
	 * 查询学生
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/4 16:40
	 */
	@GetMapping("getStudent")
	@ApiOperation("查询学生")
	public R getStudent(String name, String phone) {

		//昵称和手机号不能同时为空
		if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(phone)) {
			return new R(CommonConstants.FAIL, "昵称和手机号至少填一项", false);
		}

		List<OfficeStudentDetailVO> student = officeService.getStudent(name, phone);

		if (student.size() == 0) {
			return new R(CommonConstants.FAIL, "该学生不存在", false);
		}

		return new R(student);
	}

	/**
	 * 工作室老师批量添加学生(目前后台使用)(教学->师生管理->批量添加学生)
	 *
	 * @param request
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/4 17:08
	 */
	@PostMapping("addStudent")
	@ApiOperation("批量添加学生")
	public R addStudent(@RequestBody Map<String, Object> request) {
		return officeService.addStudent(request);
	}

}
